package com.abe.libquick.widget.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;

import com.abe.libcore.view.common.BaseJDialog;
import com.abe.libquick.R;
import com.abe.libquick.utils.NormalUtils;

import java.util.Date;

public abstract class BaseCDialog extends BaseJDialog {
    protected Context context;
    private NestedScrollView layout;
    private View btnView;
    private OnCustomDialogListener listener;
    private String title;
    private String type;
    private TextView item_name;

    public BaseCDialog(@NonNull Context context) {
        super(context);
    }

    public BaseCDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    public BaseCDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public BaseCDialog(@NonNull Context context, String title, OnCustomDialogListener listener) {
        this(context, BaseCDialog.class.getSimpleName(), title, listener);
    }

    public BaseCDialog(@NonNull Context context, String type, String title, OnCustomDialogListener listener) {
        super(context);
        this.type = type;
        this.title = title;
        this.context = context;
        this.listener = listener;
    }

    protected abstract int getChildLayoutRes();

    protected abstract void initChildContent();

    public Date stringToDate(String dateStr, String rule) {
        return NormalUtils.stringToDate(rule, dateStr);
    }

    public String dateToString(Date date, String rule) {
        return NormalUtils.dateToString(rule, date);
    }

    public String dateToString(String rule, Date date) {
        return NormalUtils.dateToString(rule, date);
    }

    public String getText(EditText editText) {
        return editText.getText().toString().trim();
    }

    public String getText(TextView textView) {
        return textView.getText().toString().trim();
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.dialog_item_layout_custom;
    }

    @Override
    protected void initContent() {
        btnView = findViewById(R.id.dialog_button_view);
        item_name = findViewById(R.id.item_name);
        item_name.setText(title);
        // 按钮
        Button cancel = findViewById(R.id.item_cancel);
        Button confirm = findViewById(R.id.item_confirm);
        cancel.setOnClickListener(view -> {
            if (listener != null) {
                listener.onClickCancel(type);
            }
        });
        confirm.setOnClickListener(view -> {
            if (listener != null) {
                listener.onClickConfirm(type);
            }
        });
        layout = findViewById(R.id.dialog_custom_view_layout);
        View view = LayoutInflater.from(context).inflate(getChildLayoutRes(), null);
        layout.addView(view);
        initChildContent();
    }

    public void setListener(OnCustomDialogListener listener) {
        this.listener = listener;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setTitle(String title) {
        this.title = title;
        if (item_name != null && !TextUtils.isEmpty(title)) {
            item_name.setText(title);
        }
    }

    protected void setBottomBtnHide() {
        btnView.setVisibility(View.GONE);
        layout.setPadding(0, 0, 0, 0);
    }

    @Override
    public void show() {
        super.show();
        float scale = 0.9f;
        if (isImp(BaseCDialog.this.getClass(), WidthScaleListener.class)) {
            scale = ((WidthScaleListener) BaseCDialog.this).widthScale();
        }
        Window dialogWindow = getWindow();
        if (dialogWindow == null) return;
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = getContext().getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * scale); // 宽度设置为屏幕的0.8
        dialogWindow.setAttributes(lp);
    }

    /**
     * 是否实现接口
     *
     * @param a 类
     * @param b 接口
     */
    protected Boolean isImp(Class<?> a, Class<?> b) {
        return b.isAssignableFrom(a);
    }

    public interface OnCustomDialogListener {
        void onClickCancel(String type);

        void onClickConfirm(String type);
    }

    public interface WidthScaleListener {
        float widthScale();
    }
}